﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using FluentValidation;
using FluentValidation.Results;
using System.Data.Objects.DataClasses;
using STLGROUP.Layers.DataAccessLayers.Validators;
using STLGROUP.Layers.Common;

namespace STLGROUP.Layers.DataAccessLayers
{
    /// <summary>
    /// Validation field.
    /// </summary>
    /// <author>Viet sang</author>
    /// <date>21/10/2011</date>

    public partial class QuanLyBanHangEntities: ObjectContext
    {
        #region Variables

        private bool _validationSucceeded = false;
        IList<ValidationFailure> _failures = null;

        #endregion

        partial void OnContextCreated()
        {
            this.SavingChanges += new EventHandler(QuanLyBanHangEntities_SavingChanges);

        }

        #region Validate Entities

        public void QuanLyBanHangEntities_SavingChanges(object sender, EventArgs e)
        {
            var stateManager = ((QuanLyBanHangEntities)sender).ObjectStateManager;

            _failures = null;

            var changedEntities = ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Added);

            foreach (ObjectStateEntry item in changedEntities)
            {   
                #region DT_KhachHang

                if (item.Entity is DT_KhachHang)
                {
                    DT_KhachHang khachHang = (DT_KhachHang)item.Entity;

                    if (khachHang.NgayTao == null && khachHang.NguoiTao == null && khachHang.EntityState == EntityState.Added)
                    {
                        khachHang.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        khachHang.NgayTao = DateTime.Now;
                    }
                    
                    CustomerValidator validator = new CustomerValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(khachHang);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region DT_NhanVien
                
                if (item.Entity is DT_NhanVien)
                {
                    DT_NhanVien nhanVien = (DT_NhanVien)item.Entity;

                    if (nhanVien.NgayTao == null && nhanVien.NguoiTao == null && nhanVien.EntityState == EntityState.Added)
                    {
                        nhanVien.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        nhanVien.NgayTao = DateTime.Now;
                    }

                    DT_NhanVienValidator validator = new DT_NhanVienValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(nhanVien);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;
                    
                }

                #endregion

                #region ACL_Module

                if (item.Entity is ACL_Module)
                {
                    ACL_Module module = (ACL_Module)item.Entity;

                    if (module.NgayTao == null && module.NguoiTao == null && module.EntityState == EntityState.Added)
                    {
                        module.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        module.NgayTao = DateTime.Now;
                    }

                    ACL_ModuleValidator validator = new ACL_ModuleValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(module);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region ACL_NhomNguoiDung

                if (item.Entity is ACL_NhomNguoiDung)
                {
                    ACL_NhomNguoiDung entity = (ACL_NhomNguoiDung)item.Entity;

                    if (entity.NgayTao == null && entity.NguoiTao == null && entity.EntityState == EntityState.Added)
                    {
                        entity.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        entity.NgayTao = DateTime.Now;
                    }

                    ACL_NhomNguoiDungValidator validator = new ACL_NhomNguoiDungValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(entity);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region ACL_PhanQuyenNhomNguoiDung

                if (item.Entity is ACL_PhanQuyenNhomNguoiDung)
                {
                    ACL_PhanQuyenNhomNguoiDung entity = (ACL_PhanQuyenNhomNguoiDung)item.Entity;

                    if (entity.NgayTao == null && entity.NguoiTao == null && entity.EntityState == EntityState.Added)
                    {
                        entity.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        entity.NgayTao = DateTime.Now;
                    }

                    ACL_PhanQuyenNhomNguoiDungValidator validator = new ACL_PhanQuyenNhomNguoiDungValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(entity);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region DM_ChucVu

                if (item.Entity is DM_ChucVu)
                {
                    DM_ChucVu entity = (DM_ChucVu)item.Entity;

                    if (entity.NgayTao == null && entity.NguoiTao == null && entity.EntityState == EntityState.Added)
                    {
                        entity.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        entity.NgayTao = DateTime.Now;
                    }

                    DM_ChucVuValidator validator = new DM_ChucVuValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(entity);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region DM_TinhThanh

                if (item.Entity is DM_TinhThanh)
                {
                    DM_TinhThanh entity = (DM_TinhThanh)item.Entity;

                    if (entity.NgayTao == null && entity.NguoiTao == null && entity.EntityState == EntityState.Added)
                    {
                        entity.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        entity.NgayTao = DateTime.Now;
                    }

                    DM_TinhThanhValidator validator = new DM_TinhThanhValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(entity);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region DT_NhaCungCap

                if (item.Entity is DT_NhaCungCap)
                {
                    DT_NhaCungCap nhaCungCap = (DT_NhaCungCap)item.Entity;

                    if (nhaCungCap.NgayTao == null && nhaCungCap.NguoiTao == null && nhaCungCap.EntityState == EntityState.Added)
                    {
                        nhaCungCap.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        nhaCungCap.NgayTao = DateTime.Now;
                    }

                    if (nhaCungCap.NgayTao == null && nhaCungCap.NguoiTao == null && nhaCungCap.EntityState == EntityState.Added)
                    {
                        nhaCungCap.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        nhaCungCap.NgayTao = DateTime.Now;
                    }

                    DT_NhaCungCapValidator validator = new DT_NhaCungCapValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(nhaCungCap);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region DT_SanPham

                if (item.Entity is DT_SanPham)
                {
                    DT_SanPham sanPham = (DT_SanPham)item.Entity;

                    if (sanPham.NgayTao == null && sanPham.NguoiTao == null && sanPham.EntityState == EntityState.Added)
                    {
                        sanPham.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        sanPham.NgayTao = DateTime.Now;
                    }

                    if (sanPham.NgayTao == null && sanPham.NguoiTao == null && sanPham.EntityState == EntityState.Added)
                    {
                        sanPham.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        sanPham.NgayTao = DateTime.Now;
                    }

                    DT_SanPhamValidator validator = new DT_SanPhamValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(sanPham);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region KM_ChuongTrinhKhuyenMai

                if (item.Entity is KM_ChuongTrinhKhuyenMai)
                {
                    KM_ChuongTrinhKhuyenMai obj = (KM_ChuongTrinhKhuyenMai)item.Entity;

                    if (obj.NgayTao == null && obj.NguoiTao == null && obj.EntityState == EntityState.Added)
                    {
                        obj.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        obj.NgayTao = DateTime.Now;
                    }

                    KM_ChuongTrinhKhuyenMaiValidator validator = new KM_ChuongTrinhKhuyenMaiValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(obj);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region KM_GiamGiaHoaDon

                if (item.Entity is KM_GiamGiaHoaDon)
                {
                    KM_GiamGiaHoaDon obj = (KM_GiamGiaHoaDon)item.Entity;

                    if (obj.NgayTao == null && obj.NguoiTao == null && obj.EntityState == EntityState.Added)
                    {
                        obj.NguoiTao = UICommon.CurrentUserInfo.IDNhanVien;
                        obj.NgayTao = DateTime.Now;
                    }

                    KM_GiamGiaHoaDonValidator validator = new KM_GiamGiaHoaDonValidator();
                    FluentValidation.Results.ValidationResult results = validator.Validate(obj);

                    _validationSucceeded = results.IsValid;
                    _failures = results.Errors;

                }

                #endregion

                #region Throw exception

                if (!_validationSucceeded)
                {
                    if (_failures != null)
                    {
                        ThrowException(_failures);                        
                    }
                }

                #endregion

            }
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Throw exception
        /// </summary>
        /// <param name="errors"></param>
        private void ThrowException(IList<ValidationFailure> errors)
        {
            if (errors.Count > 0)
            {
                string err = string.Empty;

                foreach (ValidationFailure val in errors)
                {
                    err += "- " + val.PropertyName + " " + val.ErrorMessage + '\n';
                }

                throw new System.ComponentModel.DataAnnotations.ValidationException(err);
            }
        }

        #endregion

    }
}
